// pages/order-detail/order-detail.js
var util = require('../../utils/util.js')
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var uid = 0;
var token = '';
var load = true;
var tc = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    util: util,
    tc: 0,
    uid: 0,  //当前登录用户Id
    token: '', //goAtherPagegoAtherPage
    order: {},
    orderid : '',
    orderType:2, //订单类型：1:卖出 2:买入

    pinglun:'',
    huiping:'',
    zhuiping:'',

    scroll_x_w: 220
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var tc = app.get_tc();
    var titlename = tc == 1 ? '訂單詳情' : '订单详情';
    wx.setNavigationBarTitle({
      title: titlename,
    })
    load = true;
    var that = this;
    var inviteuid = options.inviteuid;
    tc = app.get_tc();
    console.log("orderid:", options.orderid );

    wx.getSystemInfo({
      success: function (res) {
        var screen_w = res.windowWidth;
        that.setData({
          scroll_x_w: screen_w - 96
        });
      }
    })
    
    this.setData({    
      orderid: options.orderid,
      orderType: options.ordertype ? options.ordertype : 2 ,
      tc: tc
    })   
    //调用应用实例的方法获取全局数据
    app.LoginUserInfo(function (res) {
      //更新数据
      that.setData({
        uid: res.uid,
        token: res.token
      })
      uid = res.uid;
      token = res.token;
      //获取订单
      that.getOrder(uid, token);

    }, inviteuid);  

    var titlename = tc == 1 ? '訂單詳情' : '订单详情';
    wx.setNavigationBarTitle({
      title: titlename,
    });

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
      that.resetNavColor();
    });

  
  },

  getOrder:function(uid, token){
    var that = this;
    var privatekey = utilMd5.hexMD5(uid + 'zongordercontent' + token);
    util.api({
      data: {
        publickey: util.publiccode,
        ac: "zongordercontent",
        uid: uid,
        privatekey: privatekey,
        orderid: that.data.orderid
      },
      method: 'GET',
      success: function (responseData) {
        var console1 = tc == 1 ? '訂單詳情：' : '订单详情';
        console.log(console1, responseData.data);
        var data = responseData.data;
        if (data.paytime && data.paytime != '0') {
          data.paytime = util.formatTime(new Date(data.paytime * 1000), '-', ':');
        }
        that.setData({
          order: data
        });

      }
    });   
  },

  

  repayOldOrder: function (event) {
    var that = this;
    var order = event.currentTarget.dataset.order;
    var orderid = order.out_trade_no;
    var privatekey = utilMd5.hexMD5(this.data.uid + 'zongoldpay' + this.data.token);

    var data = {
      'ac': 'zongoldpay',
      'orderid': orderid,
      'binduid': util.orgid,
      'uid': this.data.uid,
      'privatekey': privatekey
    };
    console.log("支付老订单参数：", data);
    util.api({
      data: data,
      method: 'POST',
      success: function (responseData) {
        var data = responseData.data;

        console.log("支付老订单返回：", data);
        if (data.res === true) {
          //挂起微信支付请求
          var orderData = data.data;
          wx.requestPayment({

            'timeStamp': orderData.timeStamp,
            'nonceStr': orderData.nonceStr,
            'package': orderData.package,
            'signType': orderData.signType,
            'paySign': orderData.paySign,
            'success': function (res) {
              app.dialog('支付成功', 'success', function () {
                //刷新
                that.getOrder(uid, token);
              });
              
              if (parseInt(orderData.add_source) > 0) {
                var toice1 = tc == 1 ? '恭喜妳獲得' : '恭喜你获得';
                var toice2 = tc == 1 ? '積分' : '积分';
                var toice = toice1 + orderData.add_source + toice2;
                wx.showModal({
                  title: '支付成功',
                  content: toice,
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {

                    }
                  }
                })
              }
            },
            'fail': function (res) {
              console.log(res);
              if (res.errMsg == 'requestPayment:fail cancel') {

              } else {
                app.dialog(res.errMsg, 'loading');
              }
            }
          });

        } else {
          app.dialog('购买失败', 'loading');
        }
      }
    });
  },

  
  cancelorder: function (event) {
    var that = this;
    var order = event.currentTarget.dataset.order;
    console.log(order);
    var privatekey = utilMd5.hexMD5(this.data.uid + 'cancelgoodsorder' + this.data.token);
    var data = {
      publickey: util.publiccode,
      ac: "cancelgoodsorder",
      reason: '默认取消',
      orderid: this.data.orderid,
      uid: this.data.uid,
      privatekey: privatekey
    };
    console.log(data);
    //下单请求
    util.api({
      data: data,
      method: 'POST',
      success: function (responseData) {
        var data = responseData.data;
        console.log("cancel order response:", data);
        if(data.res){
          //获取订单
          that.getOrder(uid, token);
        } else {
          app.dialog(data.msg, 'loading');
        }
      }
    });
  },

  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    console.log("goAtherPage link = " + url);
    wx.navigateTo({ url: url })
  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    console.log("goAtherPageRedirect link = " + url);
    wx.redirectTo({ url: url })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!load){
      //获取订单
      this.getOrder(uid, token);
    }
    load = false;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },
  showEWM: function (e) {
    var shareImgUrl = e.currentTarget.dataset.imgsrc;
    console.log("图片链接：" + shareImgUrl);
    wx.previewImage({
      current: shareImgUrl,
      urls: [shareImgUrl]
    })
  },
})